package com.xuecheng.system.controller;

import com.xuecheng.commons.model.dto.CompanyAuditDto;
import com.xuecheng.commons.model.dto.CompanyDateDto;
import com.xuecheng.commons.model.dto.CompanyDto;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.vo.CompanyVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.system.domain.Company;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.system.service.CompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;

/**
 * <p>
 * 教育机构前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService  companyService;

    @GetMapping("/mine")
    public ResponseResult mine(){
        //从线程对象获取公司id 查询对应机构
        Company company = companyService.getById(AuthInfoHolder.getCompanyId());
        // 构造返回 BeanHelper 只能处理同名 同类型的返回值
        CompanyVo companyVo = BeanHelper.copyProperties(company, CompanyVo.class);
        // 返回
        return ResponseResult.okResult(companyVo);
    }

    /**
     * 分页查询机构列表
     * 请求路径：postMapping  /system/company/list
     *请求参数 PageRequest pageRequest ,
     */
    @PostMapping("/list")
    public ResponseResult companyList(PageRequest pageRequest, @RequestBody CompanyDto dto){
        return companyService.companyList(pageRequest,dto);
    }

    /**
     *根据id查询机构
     * 请求路径 get  /system/company/{id}
     * 请求参数  id
     */
    @GetMapping("/{id}")
    public ResponseResult findById(@PathVariable("id") Long id){
        Company company = companyService.getById(id);
        CompanyVo companyVo = BeanHelper.copyProperties(company, CompanyVo.class);
        return ResponseResult.okResult(companyVo);
    }

    /**
     * 提交资料
     * 请求方式 post
     * 请求路径 /system/company
     * 参数   body
     */
    @PostMapping
    public ResponseResult company(@RequestBody CompanyDateDto dto){
         return  companyService.company(dto);
    }

    /**
     * 审批机构
     * 请求方式 POST
     * 请求路径： /system/company/approve
     */
    @PostMapping("/approve")
    public ResponseResult approve(@RequestBody CompanyAuditDto dto){
        Company company = companyService.getById(dto.getCompanyId());
        company.setApprovalStatus(dto.getApprovalStatus());
        company.setApprovalComment(dto.getApprovalComment());
        companyService.updateById(company);
        return ResponseResult.okResult();
        
    }


}
